<script setup lang="ts">
import { MsgType, PrescriptionStatus } from '@/enums'
import type { Message, Prescription } from '@/types/room'
// import { flagOptions, timeOptions } from '@/services/constants'
import { fn, fn2 } from '@/utils/filter'
import type { Image } from '@/types/consult'
import { showImagePreview, showToast } from 'vant'
import { useUserStore } from '@/stores'
import dayjs from 'dayjs'
// import { getPrescriptionPic } from '@/services/consult'
import EvaluateCard from './EvaluateCard.vue'
import { useShowPrescription } from '@/composable'
import router from '@/router'

defineProps<{
  list: Message[]
}>()

// arr.find(item => item.value === msg.consultRecord.illnessTime).label

// const fn = (value: IllnessTime) => {
//   return timeOptions.find((item) => item.value === value)?.label
// }
// const fn2 = (value: 0 | 1) => {
//   return flagOptions.find((item) => item.value === value)?.label
// }

const showImage = (arr?: Image[]) => {
  console.log(arr)
  // 预览照片
  // console.log(ImagePreview)
  // showImagePreview([
  //   'https://img2.baidu.com/it/u=3096699763,2795196671&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1702141200&t=8613cb997b9c305f1ef1eafa0a731b03',
  //   'https://img2.baidu.com/it/u=780799596,2042506373&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1702141200&t=abaad03e2578415a90ae467aa3a0d0dc'
  // ])
  if (arr && arr.length) showImagePreview(arr.map((item) => item.url))
}

const store = useUserStore()
// 查看处方
const { showPrescription } = useShowPrescription()
// const showPrescription = async (id: string) => {
//   // 请求处方单的url => 调用接口
//   const res = await getPrescriptionPic(id)
//   console.log(res.data.url)

//   showImagePreview([res.data.url])
// }

const buy = (pre: Prescription) => {
  console.log(pre) // 买药逻辑...

  if (pre) {
    // 有处方

    if (pre.status === PrescriptionStatus.Invalid) {
      // 处方失效了
      return showToast('处方已失效')
    }
    if (pre.status === PrescriptionStatus.NotPayment && !pre.orderId) {
      // 处方未付款 还没有创建过订单 => 创建订单页面(预支付)再支付
      return router.push(`/order/pay?id=${pre.id}`)
    }

    // 创建过订单 直接去支付
    router.push(`/order/${pre.orderId}`)
  }
}
</script>

<template>
  <!-- 空标签 template block 都是用来分担逻辑 -->
  <template
    v-for="{ msgType, msg, id, createTime, from, fromAvatar } in list"
    :key="id"
  >
    <!-- 病情描述 -->
    <div class="msg msg-illness" v-if="msgType === MsgType.CardPat">
      <div class="patient van-hairline--bottom" v-if="msg.consultRecord">
        <p>
          {{ msg.consultRecord.patientInfo.name }}
          {{ msg.consultRecord.patientInfo.genderValue }}
          {{ msg.consultRecord.patientInfo.age }}岁
        </p>
        <p>
          {{ fn(msg.consultRecord.illnessTime) }} |
          {{ fn2(msg.consultRecord.consultFlag) }}
        </p>
      </div>
      <van-row>
        <van-col span="6">病情描述</van-col>
        <van-col span="18">{{ msg.consultRecord?.illnessDesc }}</van-col>
        <van-col span="6">图片</van-col>
        <van-col span="18" @click="showImage(msg.consultRecord?.pictures)">
          点击查看
        </van-col>
      </van-row>
    </div>
    <!-- 温馨提示 -->
    <div class="msg msg-tip" v-if="msgType === MsgType.NotifyTip">
      <div class="content">
        <span class="green">温馨提示：</span>
        <span>{{ msg.content }}</span>
      </div>
    </div>
    <!-- 通用通知 -->
    <div class="msg msg-tip" v-if="msgType === 31">
      <div class="content">
        <span>{{ msg.content }}</span>
      </div>
    </div>
    <!-- 我发的消息 -->
    <div
      class="msg msg-to"
      v-if="msgType === MsgType.MsgText && store.user?.id === from"
    >
      <div class="content">
        <div class="time">{{ dayjs(createTime).format('HH:mm') }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
      <van-image :src="store.user?.avatar" />
    </div>
    <!-- 医生发的消息 -->
    <div
      class="msg msg-from"
      v-if="msgType === MsgType.MsgText && store.user?.id !== from"
    >
      <van-image :src="fromAvatar" />
      <div class="content">
        <div class="time">{{ dayjs(createTime).format('HH:mm') }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
    </div>
    <!-- 我发的照片 -->
    <div
      class="msg msg-to"
      v-if="msgType === MsgType.MsgImage && store.user?.id === from"
    >
      <div class="content">
        <div class="time">{{ dayjs(createTime).format('HH:mm') }}</div>
        <van-image fit="contain" :src="msg.picture?.url" />
      </div>
      <van-image :src="store.user?.avatar" />
    </div>

    <!-- 医生发的照片 -->
    <div
      class="msg msg-from"
      v-if="msgType === MsgType.MsgImage && store.user?.id !== from"
    >
      <van-image :src="fromAvatar" />
      <div class="content">
        <div class="time">{{ dayjs(createTime).format('HH:mm') }}</div>
        <van-image fit="contain" :src="msg.picture?.url" />
      </div>
    </div>

    <!-- 处方消息 -->
    <div class="msg msg-recipe" v-if="msgType === MsgType.CardPre">
      <!-- 判断有没有处方内容 -->
      <div class="content" v-if="msg.prescription">
        <div class="head van-hairline--bottom">
          <div class="head-tit">
            <h3>电子处方</h3>
            <!-- 获取到原始处方的图片url -->
            <p @click="showPrescription(msg.prescription.id)">
              原始处方 <van-icon name="arrow"></van-icon>
            </p>
          </div>
          <p>
            {{ msg.prescription.name }}
            {{ msg.prescription.genderValue }}
            {{ msg.prescription.age }}岁
            {{ msg.prescription.diagnosis }}
          </p>
          <p>开方时间：{{ msg.prescription.createTime }}</p>
        </div>
        <div class="body">
          <div
            class="body-item"
            v-for="med in msg.prescription.medicines"
            :key="med.id"
          >
            <div class="durg">
              <p>{{ med.name }} {{ med.specs }}</p>
              <p>{{ med.usageDosag }}</p>
            </div>
            <div class="num">x{{ med.quantity }}</div>
          </div>
        </div>
        <div class="foot">
          <span @click="buy(msg.prescription)">购买药品</span>
        </div>
      </div>
    </div>

    <!-- 评价消息 非常复杂 => 不在这里写结构了 直接去组件里面写(封装组件) -->
    <!-- <Xxx v-if="msgType === MsgType.CardEvaForm"></Xxx> -->
    <!-- <EvaluateCard
      v-if="msgType === MsgType.CardEvaForm || msgType === MsgType.CardEva"
    ></EvaluateCard> -->

    <div
      class="msg msg-comment"
      v-if="msgType === MsgType.CardEva || msgType === MsgType.CardEvaForm"
    >
      <!-- msg.evaluateDoc 评价了就有这个字段 没有评价没有这个字段 -->
      <evaluate-card :evaluateDoc="msg.evaluateDoc" />
    </div>

    <!-- 结束消息 -->
    <div
      class="msg msg-tip msg-tip-cancel"
      v-if="msgType === MsgType.NotifyCancel"
    >
      <div class="content">
        <span>{{ msg.content }}</span>
      </div>
    </div>
  </template>
</template>

<style lang="scss" scoped>
@import '@/styles/room.scss';
</style>
